<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../stores/user'
import { ElMessage } from 'element-plus'

const router = useRouter()
const userStore = useUserStore()

const loginForm = reactive({
  username: '',
  password: ''
})

const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
}

const loading = ref(false)
const formRef = ref(null)

const handleLogin = async () => {
  if (!formRef.value) return
  
  await formRef.value.validate(async (valid) => {
    if (valid) {
      try {
        loading.value = true
        await userStore.login(loginForm)
        await userStore.getUserInfo()
        ElMessage.success('登录成功')
        router.push('/')
      } catch (error) {
        console.error('登录失败:', error)
        ElMessage.error(error.response?.data?.message || '登录失败，请检查用户名和密码')
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-title">企业员工管理系统</div>
      <el-form
        ref="formRef"
        :model="loginForm"
        :rules="rules"
        label-position="top"
        size="large"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            prefix-icon="User"
          />
        </el-form-item>
        
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            @keyup.enter="handleLogin"
          />
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="primary"
            :loading="loading"
            class="login-button"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  width: 100vw;
  background: url('https://img1.baidu.com/it/u=4055151126,1036544537&fm=253&fmt=auto&app=138&f=JPEG?w=1200&h=437') center center/cover no-repeat;
  background-color: #f5f7fa;
  overflow: hidden;
}

.login-box {
  width: 400px;
  padding: 40px 36px 32px 36px;
  background: rgba(255, 255, 255, 0.85);
  border-radius: 18px;
  box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.18);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: 1px solid rgba(255,255,255,0.18);
}

.login-title {
  text-align: center;
  font-size: 32px;
  font-weight: 800;
  margin-bottom: 36px;
  color: #1976d2;
  letter-spacing: 2px;
  text-shadow: 0 2px 8px rgba(25, 118, 210, 0.08);
}

.el-form-item {
  margin-bottom: 28px;
}

.el-input {
  font-size: 18px;
  padding: 10px 12px;
  border-radius: 8px;
}

.el-input__wrapper {
  border-radius: 8px;
}

.login-button {
  width: 100%;
  font-size: 20px;
  height: 48px;
  border-radius: 8px;
  background: linear-gradient(90deg, #1976d2 0%, #42a5f5 100%);
  color: #fff;
  font-weight: bold;
  box-shadow: 0 2px 8px rgba(25, 118, 210, 0.12);
  transition: background 0.3s, box-shadow 0.3s;
}
.login-button:hover {
  background: linear-gradient(90deg, #1565c0 0%, #64b5f6 100%);
  box-shadow: 0 4px 16px rgba(25, 118, 210, 0.18);
}
</style> 